import * as step from '@flow-step/step-toolkit'
import dockerToolkit from '@flow-step/dockertoolkit'
import {IParams} from './params';
import {copyFileSync, existsSync, readFileSync} from 'fs';
import {join} from 'path';
import process from "process";
import {generateOptionsArray} from './util'

export async function buildPushImage(params: IParams): Promise<void> {
    const projectDir = params.projectDir;
    process.chdir(projectDir);

    const dockerfilePath = params.dockerfilePath;
    let contextPath = params.contextPath
    contextPath = dockerToolkit.formatContextPath(contextPath, dockerfilePath, projectDir);
    step.info(`contextPath is set to: ${contextPath}`);

    const dockerfileContent = readFileSync(dockerfilePath, 'utf-8');
    step.info(`-----------Dockerfile Content----------\n${dockerfileContent}-----------Dockerfile Content----------`)

    step.info(`docker image is set to: ${params.getPublicImage()}`);
    step.info(`docker vpc image is set to: ${params.getVpcImage()}`);

    let cacheType = params.cacheType;
    if (cacheType === undefined) {
        cacheType = "remote"
    }

    let args = dockerToolkit.getDockerBuildArgs(params.getPushImage(), dockerfilePath, contextPath, cacheType, params.cacheImageId as string);
    const options = params.options as string
    if (options) {
        const optionsArray = generateOptionsArray(options)
        args = args.concat(optionsArray)
    }

    step.info(`ready to execute: docker ${args.join(" ")}`)

    const resultCode = await step.exec.call('docker', args, {
        listener: {
            stdoutLine: (line: string) => step.info(line),
            stderrLine: (line: string) => step.info(line),
        }
    });
    if (resultCode !== 0) {
        throw new Error(`build image failed, exit code: ${resultCode}`);
    }

    const dockerTag = params.dockerTag;
    // export docker artifact for gui
    step.artifact.exportArtifactDocker(`${params.stepID}.DOCKER_TAG`, new step.artifact.ArtifactDocker({
        artifact: "DOCKER_TAG",
        image: params.getPublicImage(),
        dockerTag: dockerTag
    }));

    step.artifact.exportArtifactDocker(`${params.stepID}.DOCKER_OUTPUT`, new step.artifact.ArtifactDocker({
        artifact: "DOCKER_OUTPUT",
        image: params.getPublicImage(),
        dockerTag: dockerTag
    }));

    step.artifact.exportArtifactDocker(`${params.stepID}.DOCKER_OUTPUT_VPC`, new step.artifact.ArtifactDocker({
        artifact: "DOCKER_OUTPUT_VPC",
        image: params.getVpcImage(),
        dockerTag: dockerTag
    }));

    // export docker artifact for pac
    if (params.artifact) {
        step.artifact.exportArtifactDocker(`${params.stepID}.artifact`, new step.artifact.ArtifactDocker({
            artifact: `${params.artifact}`,
            image: params.getPublicImage(),
            dockerTag: dockerTag
        }));

        step.artifact.exportArtifactDocker(`${params.stepID}.artifact_vpc`, new step.artifact.ArtifactDocker({
            artifact: `${params.artifact}_vpc`,
            image: params.getVpcImage(),
            dockerTag: dockerTag
        }));
    }
}